<script setup lang="ts">
import { ElMessage } from 'element-plus'
import { ref } from 'vue'
import http from '../../api/http'
import type { RenderContentContext, TreeInstance } from 'element-plus'
import type { Iauth,ISetauth } from '@/types/Auth'
interface Tree {
  id: number
  label: string
  children?: Tree[]
}
var dialogVisible=defineModel();
const emit = defineEmits<{ (e: 'refresh'): void }>()
const porps =defineProps(["rid"]);
type Node = RenderContentContext['node']
const treeRef = ref<TreeInstance>()
const getCheckedKeys = () => {
   var wet=treeRef.value!.getHalfCheckedKeys()
      var tyu=treeRef.value!.getCheckedKeys(false)
      wet.forEach(res=>{
       tyu.push(res)
      })
      var cc={ids:tyu,rid:porps.rid}
  console.log(cc)
  http.post('Authority',cc).then(res => {
                if (res.code == '1') {
                    ElMessage({
                        message: res.msg,
                        type: 'success',
                    })
                    dialogVisible.value = false
                    emit('refresh')
                }
                else {
                    ElMessage({
                        message: res.msg,
                        type: 'error',
                    })
                }

            }).catch(err => {
                console.log('err: ', err);
            })
}

var hs = () => {
        var uu =[]
        var opk=[1,2,3,4]
        var jk=[]
        http.get<ISetauth[]>('Authority',{rid:porps.rid}).then(res=>{
   res.data.forEach(c=>{
      uu.push(opk.includes(c.menuID)?'':c.menuID)
     
   })
  uu.forEach(data=>{
           if(data!=""){
          jk.push(data)
           }
           })
            console.log('jk',jk)
          
treeRef.value!.setCheckedKeys(jk, false)
   })
}



const resetChecked = () => {
  treeRef.value!.setCheckedKeys([], false)
}

const defaultProps = {
  children: 'children',
  label: 'label',
}
const data:Tree[]=reactive([])
watch(()=>porps.rid,(value)=>{
hs()

})
var search=()=>{
   http.get<Iauth[]>('Menus/sy')
        .then(res => {
        res.data.forEach(v=>{
              if (v.parentId == 0) {
          var node = {
            id: v.menuId,
            label: v.menuName,
            children: []
          };
         
             data.push(node);
          res.data.forEach(vv => {
            if (vv.parentId == v.menuId) {
              node.children.push({
                id: vv.menuId,
                label: vv.menuName
              });
            }
          });


        }

        })

         });
     

      

}

onMounted(()=>{

    search();
    
 
}





    
)
</script>
<template>
      <el-dialog v-model="dialogVisible" :show-close="false" width="500" center>
      <el-tree
    ref="treeRef"
    style="max-width: 600px"
    :data="data"
    show-checkbox
    default-expand-all
    node-key="id"
    highlight-current
    :props="defaultProps"
    
  />

  <div class="mt-2">
   
    <el-button @click="getCheckedKeys">提交</el-button>
    <el-button @click="resetChecked">重置</el-button>
  </div>
</el-dialog>
</template>
<style scoped></style>
